package com.sinosoft.lk.core.utils;

import java.io.*;

/**
 * @Author 绫小路
 * @Date 2021/3/4
 */
public class SerializetionUtils {

  /**
   * jdk 序列化
   *
   * @param object
   * @return
   * @throws IOException
   */
  public static byte[] serializetion(Object object) throws IOException {
    ObjectOutputStream objectOutputStream = null;
    ByteArrayOutputStream byteArrayOutputStream = null;
    try {
      byteArrayOutputStream = new ByteArrayOutputStream();
      objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
      objectOutputStream.writeObject(object);
      byte[] getByte = byteArrayOutputStream.toByteArray();
      return getByte;
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      objectOutputStream.close();
      byteArrayOutputStream.close();
    }
    return null;
  }

  /**
   * jdk 反序列化
   *
   * @param binaryByte
   * @return
   * @throws IOException
   */
  public static Object unserizlize(byte[] binaryByte) throws IOException {
    ObjectInputStream objectInputStream = null;
    ByteArrayInputStream byteArrayInputStream = null;
    byteArrayInputStream = new ByteArrayInputStream(binaryByte);
    try {
      objectInputStream = new ObjectInputStream(byteArrayInputStream);
      Object obj = objectInputStream.readObject();
      return obj;
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      objectInputStream.close();
      byteArrayInputStream.close();
    }
    return null;
  }
}
